package whu.cn.whushare.ui.main.known;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.TextView;

import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;

import java.io.InputStream;
import java.net.URL;

import whu.cn.whushare.R;
import whu.cn.whushare.ui.common.BaseActivity;
import whu.cn.whushare.util.MyTagHandler;

/**
 * 文件名：QuestionShow
 * 描 述：用于展示具体问题，包括问题名和问题内容
 * 作 者：胡平平
 * 时 间：2015/5/4
 */
@ViewMapping(id= R.layout.activity_show)            //获取activity_show布局文件
public class QuestionShow extends BaseActivity {
    @ViewMapping(id= R.id.activity_content)         //获取activity_content文本框
    private TextView mQuestionContent;
    @ViewMapping(id=R.id.activity)                  //获取“问题名”文本框
    private TextView mQuestionName;
    private String questionContent;
    private String questionName;
    private String html2="<p>";

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        getIntentData();    //获取问题名和问题内容
        initView();
    }

    //初始化views，展示问题标题和内容
    private void initView() {
        View root= ViewMappingUtil.mapView(this, this);
        getTopBar(root);
        mTopBar.title.setText("问题展示");
        final String html= questionContent+"";
        if(html!=""&&html.length()>3) {
             if (html.substring(0, 3).equals("<p>"))
             {
                String[] html1 = html.split(">");
                for (int i = 1; i < html1.length; i++) {
                    if(html1[i].startsWith("<"))
                    html2 = html2 + html1[i] + ">" + "<br>";
                }
            }
             else
                 html2=html;
        }
        else {
            html2 = html;
        }

        final String html3=html2;

        //新建一个异步任务
        new AsyncTask<String,String,Spanned>(){
            @Override
            //在doInBackground之前执行该函数，对后台处理任务进行预处理
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            //在doInBackground之后执行该函数
            protected void onPostExecute(Spanned s) {
                super.onPostExecute(s);

               mQuestionContent.setText(s);
            }

            @Override
            protected Spanned doInBackground(String... params) {

                Spanned sp = Html.fromHtml(html3,
                        new Html.ImageGetter() {
                            @Override
                            public Drawable getDrawable(String source) {
                                source = "http://www.027team.com" + source;
                                InputStream is = null;
                                try {
                                    is = new URL(source).openStream();
                                    Drawable d = Drawable.createFromStream(is, "src");
                                    d.setBounds(0, 0, d.getIntrinsicWidth() * 3, d.getIntrinsicHeight() * 3);
                                    is.close();
                                    return d;
                                } catch (Exception e) {
                                    return null;
                                }
                            }
                        },null);
                return sp;
            }
        }.execute();
       mQuestionName.setText(questionName);
    }

    /**
     * 获取跳转到当前activity的intent信息
     * @param context - 一般为跳转前的activity
     * @param questionContent - 问题内容
     * @param questionName - 问题名
     * @return intent
     */
    public static Intent getStartIntent(Context context,String questionContent,String questionName){
        Intent i=new Intent(context, QuestionShow.class);
        i.putExtra("questionContent",questionContent);
        i.putExtra("questionName",questionName);
        return i;
    }

    //提取intent中包含的数据，主要有问题名和问题内容
    private void getIntentData(){
        if(getIntent()!=null){
            questionContent=getIntent().getStringExtra("questionContent");
            questionName=getIntent().getStringExtra("questionName");
        }
    }



}
